package com.system.borrow.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.bean.CustomerDTO;
import com.system.borrow.bean.GloData;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SharedPreferencesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * ================================================
 * 作    者：LuCenly(落尘)
 * 版    本：1.0
 * 创建日期：2016/12/2 13:51
 * 描    述：TODO
 * ================================================
 */
public class CapitalFragment extends Fragment implements View.OnClickListener {
    View myView;
    String[] xyk = {"请选择", "是", "否"};
    String[] fc = {"请选择", "无房产", "商品住宅", "商铺", "办公楼", "厂房", "在基地／自建房"};
    String[] cc = {"请选择", "无车产", "有车产", "有车但已抵押"};
    String[] gs = {"请选择", "普通企业", "国企", "世界500强", "上市企业", "其他"};
    String[] xy = {"请选择", "无信用记录", "信用记录良好", "少量逾期"};
    private Spinner spinner_xyk, spinner_fc, spinner_cc, spinner_gs, spinner_dk, spinner_xy;
    long xxyk = 0, fcc = 0, ccc = 0, dkk = 0, xyy = 0;
    private Button btn_save;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (myView == null) {
            myView = inflater.inflate(R.layout.fragment_capital, container, false);
        } else {
            ViewParent parent = myView.getParent();
            if (parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(myView);
            }
        }
        initView(myView);
        addAction();
        return myView;
    }

    private void addAction() {

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, xyk);

        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_xyk.setAdapter(adapter);
        spinner_xyk.setSelection((int) GloData.getCustomerDTO().isHaveCreditCard(), true);
        spinner_xyk.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                xxyk = position;
                GloData.getCustomerDTO().setHaveCreditCard(xxyk);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, fc);

        //设置下拉列表的风格
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_fc.setAdapter(adapter1);
        spinner_fc.setSelection((int) GloData.getCustomerDTO().getHousePropety(), true);
        spinner_fc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                fcc = position;
                GloData.getCustomerDTO().setHousePropety(fcc);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, cc);

        //设置下拉列表的风格
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_cc.setAdapter(adapter2);
        spinner_cc.setSelection((int) GloData.getCustomerDTO().getCarPropety(), true);
        spinner_cc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ccc = position;
                GloData.getCustomerDTO().setCarPropety(ccc);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, gs);

        //设置下拉列表的风格
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_gs.setAdapter(adapter3);

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, xyk);

        //设置下拉列表的风格
        adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_dk.setAdapter(adapter4);
        spinner_dk.setSelection((int) GloData.getCustomerDTO().isHaveLoan(), true);
        spinner_dk.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                dkk = position;
                GloData.getCustomerDTO().setHaveLoan(dkk);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //将可选内容与ArrayAdapter连接起来
        ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, xy);

        //设置下拉列表的风格
        adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //将adapter 添加到spinner中
        spinner_xy.setAdapter(adapter5);
        spinner_xy.setSelection((int) GloData.getCustomerDTO().getCreditCase(), true);
        spinner_xy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                xyy = position;
                GloData.getCustomerDTO().setCreditCase(xyy);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void initView(View myView) {
        spinner_xyk = (Spinner) myView.findViewById(R.id.spinner_xyk);
        spinner_fc = (Spinner) myView.findViewById(R.id.spinner_fc);
        spinner_cc = (Spinner) myView.findViewById(R.id.spinner_cc);
        spinner_gs = (Spinner) myView.findViewById(R.id.spinner_gs);
        spinner_dk = (Spinner) myView.findViewById(R.id.spinner_dk);
        spinner_xy = (Spinner) myView.findViewById(R.id.spinner_xy);
        btn_save = (Button) myView.findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {//xxyk = 0, fcc = 0, ccc = 0, dkk = 0, xyy = 0;
            case R.id.btn_save:

                if (GloData.getCustomerDTO().isHaveCreditCard() == 0) {
                    Toast.makeText(getActivity(), "请选择是否有信用卡", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (xxyk == 0) {
                        xxyk = GloData.getCustomerDTO().isHaveCreditCard();
                    }
                }

                if (GloData.getCustomerDTO().getHousePropety() == 0) {
                    Toast.makeText(getActivity(), "请选择是否有房产", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (fcc == 0) {
                        fcc = GloData.getCustomerDTO().getHousePropety();
                    }
                }

                if (GloData.getCustomerDTO().getCarPropety() == 0) {
                    Toast.makeText(getActivity(), "请选择是否有车产", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (ccc == 0) {
                        ccc = GloData.getCustomerDTO().getCarPropety();
                    }
                }
                if (GloData.getCustomerDTO().getCarPropety() == 0) {
                    Toast.makeText(getActivity(), "请选择是否有车产", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (ccc == 0) {
                        ccc = GloData.getCustomerDTO().getCarPropety();
                    }
                }
                if (GloData.getCustomerDTO().isHaveLoan() == 0) {
                    Toast.makeText(getActivity(), "请选择是否有贷款记录", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (dkk == 0) {
                        dkk = GloData.getCustomerDTO().isHaveLoan();
                    }
                }
                if (GloData.getCustomerDTO().getCreditCase() == 0) {
                    Toast.makeText(getActivity(), "请选择信用情况", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (xyy == 0) {
                        xyy = GloData.getCustomerDTO().getCreditCase();
                    }
                }
                saveData();
                break;
        }
    }

    /**
     *
     */
    private void saveData() {//xxyk = 0, fcc = 0, ccc = 0, dkk = 0, xyy = 0;
        HttpParams params = new HttpParams();
        params.put("haveCreditCard", xxyk + "");
        params.put("housePropety", fcc + "");
        params.put("carPropety", ccc + "");
        params.put("haveLoan", dkk + "");
        params.put("creditCase", xyy + "");
        OkHttpUtils.post(URLDefind.EDIT)
                .tag(this)
                .params(params)
                .cacheMode(CacheMode.NO_CACHE)
                .execute(new DialogCallback<String>(getActivity(), String.class, null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            int statusCode = jsonObject.getInt("statusCode");
                            Gson gson = new Gson();
                            if (statusCode == 200) {
                                String data = jsonObject.getString("data");
                                TypeToken<CustomerDTO> token = new TypeToken<CustomerDTO>() {
                                };
                                CustomerDTO customer = gson.fromJson(data, token.getType());
                                GloData.setCustomerDTO(customer);
                                SharedPreferencesUtil.putString(getActivity(), "custormer", "custormer", gson.toJson(customer));
                                Toast.makeText(getActivity(), "保存成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getActivity(), jsonObject.getString("message"), Toast.LENGTH_SHORT).show();
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }
}
